package com.seaside.park.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.seaside.common.bean.Result;
import com.seaside.park.bean.AboutMe;
import com.seaside.park.bean.AppVersion;
import com.seaside.park.mapper.AboutMeMapper;
import com.seaside.park.mapper.AppVersionMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="App公共信息")
@RestController
@RequestMapping("/api/info")
public class InfoAPI {

    @Autowired
    private AppVersionMapper versionmapper;

    @Autowired
    private AboutMeMapper aboutMeMapper;

    @ApiOperation(value = "保存版本信息", response = Result.class)
    @PostMapping("/version/save")
    public Result<AppVersion> saveVersion(@RequestBody AppVersion version) {
    	
    	AppVersion origin = versionmapper.selectById(1);
    	if (origin == null) {
    		version.setVersionId(1);
    		versionmapper.insert(version);

            return Result.OK("保存成功！", version);
    	} else {
    		origin.setAppVersion(version.getAppVersion());
    		origin.setDownUrl(version.getDownUrl());
    		
    		versionmapper.updateById(origin);

            return Result.OK("保存成功！", origin);
    	}
    }

    @ApiOperation(value = "关于我们", response = Result.class)
    @PostMapping("/aboutMe")
    public Result<AboutMe> aboutMe() {
        Result<AboutMe> result;
        try{
            AboutMe aboutMe = aboutMeMapper.selectById(1);
            result = Result.OK("查询成功");
            result.setData(aboutMe);
        }catch (Exception e) {
            result = Result.Error("查询错误：" + e.getMessage());
        }
        return result;
    }
    
    @ApiOperation(value = "保存关于我们", response = Result.class)
    @PostMapping("/aboutMe/save")
    public Result<AboutMe> saveAboutMe(@RequestBody AboutMe aboutMe) {
    	
    	AboutMe origin = aboutMeMapper.selectById(1);
    	if (origin == null) {
    		aboutMe.setId(1);
    		aboutMeMapper.insert(aboutMe);
            return Result.OK("保存成功！", aboutMe);
    	} else {
    		origin.setContent(aboutMe.getContent());
    		
    		aboutMeMapper.updateById(origin);
            return Result.OK("保存成功！", origin);
    	}
    	
    }
}
